<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>declare</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="control-structures.switch.html">switch</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.return.html">return</a></div>
 <div class="up"><a href="language.control-structures.html">流程控制</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="control-structures.declare" class="sect1">
 <h2 class="title"><em>declare</em></h2>
 <p class="verinfo">(PHP 4, PHP 5)</p>
 <p class="para">
  <em>declare</em> 结构用来设定一段代码的执行指令。<em>declare</em>
  的语法和其它流程控制结构相似：
  <div class="informalexample">
   <div class="example-contents">
<div class="cdata"><pre>
declare (directive)
    statement
</pre></div>
   </div>

  </div>
 </p>
 <p class="para">
  <em>directive</em> 部分允许设定 <em>declare</em>
  代码段的行为。目前只认识两个指令：<em>ticks</em>（更多信息见下面
  <a href="control-structures.declare.html#control-structures.declare.ticks" class="link">ticks</a> 指令）以及 
  <em>encoding</em>（更多信息见下面 <a href="control-structures.declare.html#control-structures.declare.encoding" class="link">encoding</a> 指令）。
 </p>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <span class="simpara">
   encoding 是 PHP 5.3.0 新增指令。
  </span>
 </p></blockquote> 
 <p class="para">
  <em>declare</em> 代码段中的 <em>statement</em>
  部分将被执行——怎样执行以及执行中有什么副作用出现取决于
  <em>directive</em> 中设定的指令。
 </p>
 <p class="para">
  <em>declare</em> 结构也可用于全局范围，影响到其后的所有代码（但如果有
  <em>declare</em> 结构的文件被其它文件包含，则对包含它的父文件不起作用）。
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;these&nbsp;are&nbsp;the&nbsp;same:<br /><br />//&nbsp;you&nbsp;can&nbsp;use&nbsp;this:<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;entire&nbsp;script&nbsp;here<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">//&nbsp;or&nbsp;you&nbsp;can&nbsp;use&nbsp;this:<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;entire&nbsp;script&nbsp;here<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 
 <div class="sect2" id="control-structures.declare.ticks">
  <h3 class="title">Ticks</h3>
  <p class="para">
   Tick（时钟周期）是一个在 <em>declare</em> 代码段中解释器每执行
   <var class="varname"><var class="varname">N</var></var> 条可计时的低级语句就会发生的事件。<var class="varname"><var class="varname">N</var></var>
   的值是在 <em>declare</em> 中的 <em>directive</em> 部分用
   <code class="code">ticks=<var class="varname"><var class="varname">N</var></var></code> 来指定的。
  </p>
  <p class="para">
   不是所有语句都可计时。通常条件表达式和参数表达式都不可计时。
  </p>
  <p class="para">
   在每个 tick 中出现的事件是由 <span class="function"><a href="function.register-tick-function.html" class="function">register_tick_function()</a></span>
   来指定的。更多细节见下面的例子。注意每个 tick 中可以出现多个事件。
  </p>
  <p class="para">
   <div class="example" id="example-134">
    <p><strong>Example #1 Tick 的用法示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;A&nbsp;function&nbsp;called&nbsp;on&nbsp;each&nbsp;tick&nbsp;event<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"tick_handler()&nbsp;called\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">register_tick_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'tick_handler'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;print(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  </p>
  <p class="para">
   <div class="example" id="example-135">
    <p><strong>Example #2 Ticks 的用法示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"tick_handler()&nbsp;called\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;print(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  </p>
  <p class="simpara">
   参见 <span class="function"><a href="function.register-tick-function.html" class="function">register_tick_function()</a></span> 和
   <span class="function"><a href="function.unregister-tick-function.html" class="function">unregister_tick_function()</a></span>。
  </p>
 </div>
 
 <div class="sect2" id="control-structures.declare.encoding">
  <h3 class="title">Encoding</h3>
  <p class="para">
   可以用 encoding 指令来对每段脚本指定其编码方式。
   <div class="example" id="example-136">
    <p><strong>Example #3 对脚本指定编码方式</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">encoding</span><span style="color: #007700">=</span><span style="color: #DD0000">'ISO-8859-1'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;code&nbsp;here<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   </div>
  </p>
  
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    当和命名空间结合起来时 declare 的唯一合法语法是
    <em>declare(encoding=&#039;...&#039;);</em>，其中 <em>...</em>
    是编码的值。而 <em>declare(encoding=&#039;...&#039;) {}</em>
    将在与命名空间结合时产生解析错误。
   </p>
  </div>
  <p class="para">
   在 PHP 5.3 中除非在编译时指定了
   <em>--enable-zend-multibyte</em>，否则 declare 中的 encoding 值会被忽略。
  </p>
  <p class="para">
   注意除非用 <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span>，否则 PHP
   不会显示出是否在编译时指定了 <em>--enable-zend-multibyte</em>。
  </p>
  <p class="para">
   参见 <a href="ini.core.html#ini.zend.script-encoding" class="link">zend.script_encoding</a>。
  </p>
  
 </div>
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="control-structures.switch.html">switch</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.return.html">return</a></div>
 <div class="up"><a href="language.control-structures.html">流程控制</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
